;var _mapModule = require('./mapModule.js');


function Room(roomName, userList) {
	this.roomName = roomName;
	this.userList = userList;
}


exports.ParsingDataToIRC = function(buf, client, socket, rooms) {
	
	var keyword = buf.toString().split(" ", 1).toString().replace(/(^\s*)|(\s*$)/gi, "");
	// console.log( 'keyword: ' + keyword);
	switch(keyword) {
		//create
		case '/create':
			
			if( client.roomKey != null) {
				console.log('\nroom create fail\n');
				return;
			}
			
			if( parseText(keyword, buf) === '' || client.name ==='No Name')
			{
				socket.write('no name!!!');
				return;
			}
			
			var bhasName = false;
			
			var allRooms = rooms.values();
			allRooms.forEach(function(room){
				if(room.roomName == parseText(keyword, buf))
				{
					socket.write('replace room name');
					console.log('name equal' + room.roomName + ',,,,,' + parseText(keyword, buf));
					bhasName = true;
					return;
				}
					
			});
			
			if (!bhasName) {
				var room = new Room(parseText(keyword, buf), new _mapModule.map());
				room.userList.put(client.id, client);
				
				client.roomKey = parseText(keyword, buf);
				rooms.put(client.roomKey, room);
				
				socket.write('room created .... room number: ' + room.roomName);
				console.log('create room');
			}

			break;

		// join
		case '/join':
			if( client.roomKey != null){
				console.log('\nroom join fail\n');
				return;
			} 

			var getRoomName = parseText(keyword, buf);
			console.log('room name:'+ getRoomName);
			
// 			
			var room = rooms.get(getRoomName.toString());

			if( room != null){
				room.userList.put(client.id, client);
				client.roomKey = room.roomName;
				
				socket.write('room joined... room number: ' + room.roomName);
				
				var allRoomsUser = room.userList.values();
				allRoomsUser.forEach(function(_client){
					_client.socket.write(client.name + ' join!!!!');
					console.log('join room');
				});
			
			}
			break;

		case '/list':
			if( client.roomKey != null){
				console.log('\nlist fail\n');
				return;
			} 

			if (rooms.size() > 0) {
				
				var allRooms = rooms.values();
				allRooms.forEach(function(room){
					socket.write('room List: ' + room.roomName + '\n');
				});
			} else {
				socket.write('room is non');
			}

			break;

		case '/nick':
			client.name = parseText(keyword, buf);
			console.log('client id: ' + client.name);
			break;

		case '/part':
			if( client.roomKey == null){
				console.log('\npart fail\n');
				return;
			}
			
			var allRoomsUser = rooms.get(client.roomKey).userList.values();
			allRoomsUser.forEach(function(_client){
					_client.socket.write(client.name + ' leave!!!!');
					console.log(client.name + ' leave!!!!');
				});
			// console.log(client.roomKey);
			var room = rooms.get(client.roomKey);
			// console.log('room: ' + room);
			room.userList.remove(client.id);
			// console.log(room.userList.size());
			client.roomKey = null;
			
			if( room.userList.size().valueOf() <= 0)
			{
				rooms.remove(room.roomName);
			}

			break;

		case '/state':
			if(!rooms.get(client.roomKey)){
				socket.write('current state unuse');
				return;	
			}
			
			socket.write('current room Name: ' + rooms.get(client.roomKey).roomName + '         ' + 'UserCount: ' + rooms.get(client.roomKey).userList.size() + '\nUserName: ' + client.name);
			break;

		case '/quit':
			socket.write('/quit');
			break;

		case '/m':
		
			if( client.roomKey == null){
				console.log('\nmessage fail\n');
				return;
			}
			
			console.log('msg');
			
			rooms.get(client.roomKey).userList.values().forEach(function(userClient){
				if( userClient.socket != socket)
				{
					userClient.socket.write(userClient.name + ': '+ parseText(keyword, buf));
				}
			});
			break;
			
		case '/pong':
			socket.emit('timeoutpong', client);
			break;

		default:
			socket.write('wrong');
			break;
	}
};


var parseText = function(keyword, buf) {
	return buf.toString().substr(keyword.toString().length, buf.toString().length - 1).replace(/(^\s*)|(\s*$)/gi, "");
};
